package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.core.service.AttendanceCalculationService;
import com.ruoyi.out.domain.UserOvertimeLeave;
import com.ruoyi.out.service.IUserOvertimeLeaveService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Yuan
 * @description 定时插入考勤记录
 * @date 2022/10/22 16:21
 */
@Component("userOvertimeLeaveTask")
public class UserOvertimeLeaveTask {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private IUserOvertimeLeaveService userOvertimeLeaveService;

    /**
     * 插入考勤记录
     */
    @Transactional
    public void insertUserOvertimeLeave(String nowDateStr){
        //查询所有用户的id
        List<Long> userIdList = sysUserMapper.selectAllUserIdByDelFlag("0");
        for (Long userId : userIdList) {
            //查询用户加班及事假时长
            UserOvertimeLeave userOvertimeLeave = userOvertimeLeaveService.selectUserOvertimeLeaveByUserIdAndNowDate(userId,nowDateStr);
            if(StringUtils.isNull(userOvertimeLeave)){
                Double allAttendanceTime = userOvertimeLeaveService.getAllAttendanceTime(nowDateStr);
                //插入数据
                userOvertimeLeave = new UserOvertimeLeave();
                userOvertimeLeave.setId(IdUtils.fastSimpleUUID());
                userOvertimeLeave.setUserId(userId);
                userOvertimeLeave.setShouldAttendanceTime(allAttendanceTime);
                userOvertimeLeave.setAttendanceTime(nowDateStr);
                userOvertimeLeaveService.insertUserOvertimeLeave(userOvertimeLeave);
            }
        }
    }
}
